package com.ding.easyme.app.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpParams;

public class HttpRemoteRequest {
	
	public static synchronized String getMethod(HttpClient client, String url) throws HttpException, IOException {
		GetMethod method = new GetMethod(url);
		try {
			client.executeMethod(method);
			String str = method.getResponseBodyAsString();
			method.releaseConnection();
			return str;
		} finally {
			if(method != null) {
				method.releaseConnection();
				method = null;
			}
		}		
	}
	
	public static synchronized boolean downloadImage(HttpClient client,
			String url, String dest) throws HttpException, IOException {
		GetMethod method = new GetMethod(url);
		InputStream is = null;
		BufferedOutputStream out = null;
		try {
			client.executeMethod(method);
			is = method.getResponseBodyAsStream();
			File img = new File(dest);
			// 生成图片
			out = new BufferedOutputStream(new FileOutputStream(img));
			byte[] buf = new byte[2048];
			int length = is.read(buf);
			while (length != -1) {
				out.write(buf, 0, length);
				length = is.read(buf);
			}
			method.releaseConnection();
			return true;
		} finally {
			if (is != null) {
				is.close();
				is = null;
			}
			if (out != null) {
				out.close();
				out = null;
			}
			if (method != null) {
				method.releaseConnection();
				method = null;
			}
		}
	}
	
	public static void main(String[] args) throws HttpException, IOException {
		HttpClient client = new HttpClient();
//		String result = getMethod(client, "http://jingyan.baidu.com/list/195");
//		getMethod(client, "http://www.baidu.com.cn");
//		getMethod(client, "http://www.baidu.com.cn");
//		getMethod(client, "http://www.baidu.com.cn");
//		System.out.println("-----------------原生---------------------");
//		System.out.println(result);
//		
//		System.out.println("-----------------iso-gbk---------------------");
//		System.out.println(new String(result.getBytes("iso-8859-1"), "gbk"));
//		
//		System.out.println("-----------------iso-utf8---------------------");
//		System.out.println(new String(result.getBytes("iso-8859-1"), "utf-8"));
//		
//		System.out.println("----------------- to gbk---------------------");
//		System.out.println(new String(result.getBytes(), "gbk"));
//		
//		System.out.println("-----------------to utf-8---------------------");
//		System.out.println(new String(result.getBytes(), "utf-8"));
		
		
		String str = "";
		GetMethod method = new GetMethod("http://labs.soso.com/app.q?app=nameshow&of=image&ie=utf-8&w=%E6%9D%8E%E6%B8%85%E7%85%A7");
		try {
			method.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
			client.executeMethod(method);
			str = method.getResponseBodyAsString();
			method.releaseConnection();
		} finally {
			if(method != null) {
				method.releaseConnection();
				method = null;
			}
		}
		System.out.println(str);
	}
}
